package com.moli.iov.admin.web.controller;

import com.moli.iov.admin.model.vo.req.CommandQueryVo;
import com.moli.iov.admin.service.IVehicleLogFacadeService;
import com.moli.iov.down.enums.OperateTypeEnum;
import com.moli.iov.down.model.dto.res.VehicleCommandResDto;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.rest.RestResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 日志
 * </p>
 *
 * @author zql
 * @since 2019/9/19 14:35
 */
@Slf4j
@RestController
@RequestMapping("/log")
public class VehicleLogController extends BaseController {

  @Autowired
  private IVehicleLogFacadeService vehicleLogFacadeService;

  /**
   * 车辆控制日志查询
   * @param commandQueryVo
   * @return
   */
  @RequestMapping("/control/list")
  public RestResponse<PageExtend<VehicleCommandResDto>> listVehicleControlLog(
      @RequestBody(required = false) CommandQueryVo commandQueryVo){
    if(commandQueryVo == null){
      commandQueryVo = new CommandQueryVo();
    }

    assembleUserInfo(commandQueryVo);

    if(commandQueryVo.getOperateTypes() == null || commandQueryVo.getOperateTypes().isEmpty()){
      commandQueryVo.setOperateTypes(OperateTypeEnum.getControlCommands());
    }

    return vehicleLogFacadeService.listVehicleCommandLog(commandQueryVo);
  }

  /**
   * 车辆设置日志查询
   * @param commandQueryVo
   * @return
   */
  @RequestMapping("/set/list")
  public RestResponse<PageExtend<VehicleCommandResDto>> listVehicleSetLog(
      @RequestBody(required = false) CommandQueryVo commandQueryVo){
    if(commandQueryVo == null){
      commandQueryVo = new CommandQueryVo();
    }

    assembleUserInfo(commandQueryVo);

    if(commandQueryVo.getOperateTypes() == null || commandQueryVo.getOperateTypes().isEmpty()){
      commandQueryVo.setOperateTypes(OperateTypeEnum.getSetCommands());
    }

    return vehicleLogFacadeService.listVehicleCommandLog(commandQueryVo);
  }

  /**
   * 车辆控制日志导出
   * @param commandQueryVo
   * @return
   */
  @RequestMapping("/control/export")
  public void vehicleControllExport(CommandQueryVo commandQueryVo,
      HttpServletRequest req, final HttpServletResponse res)
      throws Exception {
    if(commandQueryVo == null){
      commandQueryVo = new CommandQueryVo();
    }
    assembleUserInfo(commandQueryVo);

    if(commandQueryVo.getOperateTypes() == null || commandQueryVo.getOperateTypes().isEmpty()){
      commandQueryVo.setOperateTypes(OperateTypeEnum.getControlCommands());
    }

    vehicleLogFacadeService.vehicleControllExport(commandQueryVo, req, res);
  }

  /**
   * 车辆设置日志导出
   * @param commandQueryVo
   * @return
   */
  @RequestMapping("/set/export")
  public void vehicleSetExport(CommandQueryVo commandQueryVo,
      HttpServletRequest req, final HttpServletResponse res)
      throws Exception {
    if(commandQueryVo == null){
      commandQueryVo = new CommandQueryVo();
    }

    assembleUserInfo(commandQueryVo);

    if(commandQueryVo.getOperateTypes() == null || commandQueryVo.getOperateTypes().isEmpty()){
      commandQueryVo.setOperateTypes(OperateTypeEnum.getSetCommands());
    }

    vehicleLogFacadeService.vehicleSetExport(commandQueryVo, req, res);
  }
}
